import {
  getStorageToken,
  removeStorageToken,
  setStorageToken
} from '@/utils/storage'

import { getUserInfo } from '@/api/user'

// import { Toast } from 'vant'
// 用户模块的vuex操作
export default {
  namespaced: true,
  state: {
    token: getStorageToken(),
    userInfo: {}
  },
  mutations: {
    setUserInfo(state, payload) {
      state.userInfo = payload
    },
    setToken(state, payload) {
      state.token = payload

      // 把token数据给本地存储起来
      setStorageToken(payload)
    },
    removeToken(state) {
      state.token = {}

      //   把本地存储的token都删除掉
      removeStorageToken()
    }
  },
  actions: {
    // 异步操作
    async Actions_getUserInfo(context) {
      try {
        const res = await getUserInfo()
        context.commit('setUserInfo', res.data)
      } catch {
        // 获取用户信息失败
        // 提示下
        // Toast('获取用户信息失败')
      }
    }
  }
}
